from rest_framework import serializers
from django.contrib.auth.models import User
from .models import (
    GitLabServer, Project, Environment, UserProjectPermission,
    GitLabProject, GitLabUser, GitLabAudit, DevOpsTimeline
)


class GitLabServerSerializer(serializers.ModelSerializer):
    """GitLab服务器序列化器"""
    class Meta:
        model = GitLabServer
        fields = [
            'id', 'name', 'url', 'token', 'description', 'is_active',
            'status', 'project_count', 'user_count', 'last_sync',
            'last_check', 'created_at', 'updated_at'
        ]
        extra_kwargs = {
            'token': {'write_only': True}
        }


class GitLabServerListSerializer(serializers.ModelSerializer):
    """GitLab服务器列表序列化器"""
    class Meta:
        model = GitLabServer
        fields = [
            'id', 'name', 'url', 'description', 'is_active',
            'status', 'project_count', 'user_count', 'last_sync',
            'last_check', 'created_at', 'updated_at'
        ]


class ProjectSerializer(serializers.ModelSerializer):
    """项目序列化器"""
    class Meta:
        model = Project
        fields = ['id', 'name', 'code', 'description', 'is_active', 'created_at', 'updated_at']


class EnvironmentSerializer(serializers.ModelSerializer):
    """环境序列化器"""
    class Meta:
        model = Environment
        fields = ['id', 'name', 'code', 'description', 'is_active', 'created_at', 'updated_at']


class UserProjectPermissionSerializer(serializers.ModelSerializer):
    """用户项目权限序列化器"""
    user_username = serializers.ReadOnlyField(source='user.username')
    project_name = serializers.ReadOnlyField(source='project.name')
    environment_name = serializers.ReadOnlyField(source='environment.name')

    class Meta:
        model = UserProjectPermission
        fields = [
            'id', 'user', 'user_username', 'project', 'project_name',
            'environment', 'environment_name', 'can_view', 'can_edit',
            'can_delete', 'can_deploy', 'created_at', 'updated_at'
        ]


class GitLabProjectSerializer(serializers.ModelSerializer):
    """GitLab项目序列化器"""
    gitlab_server_name = serializers.ReadOnlyField(source='gitlab_server.name')
    project_name = serializers.ReadOnlyField(source='project.name')
    environment_name = serializers.ReadOnlyField(source='environment.name')

    class Meta:
        model = GitLabProject
        fields = [
            'id', 'gitlab_server', 'gitlab_server_name', 'project', 'project_name',
            'environment', 'environment_name', 'gitlab_id', 'name', 'path',
            'path_with_namespace', 'description', 'web_url', 'ssh_url_to_repo',
            'http_url_to_repo', 'visibility', 'default_branch', 'star_count',
            'forks_count', 'last_activity_at', 'created_at', 'updated_at'
        ]


class GitLabProjectUpdateSerializer(serializers.ModelSerializer):
    """GitLab项目更新序列化器 - 只允许更新部分字段"""

    class Meta:
        model = GitLabProject
        fields = ['project', 'environment', 'visibility']


class GitLabUserSerializer(serializers.ModelSerializer):
    """GitLab用户序列化器"""
    gitlab_server_name = serializers.ReadOnlyField(source='gitlab_server.name')
    local_user_username = serializers.ReadOnlyField(source='local_user.username', allow_null=True)

    class Meta:
        model = GitLabUser
        fields = [
            'id', 'gitlab_server', 'gitlab_server_name', 'local_user', 'local_user_username',
            'gitlab_id', 'username', 'name', 'email', 'state', 'is_admin',
            'avatar_url', 'web_url', 'created_at', 'updated_at'
        ]


class GitLabAuditSerializer(serializers.ModelSerializer):
    """GitLab操作审计序列化器"""
    gitlab_server_name = serializers.ReadOnlyField(source='gitlab_server.name')
    gitlab_server_url = serializers.ReadOnlyField(source='gitlab_server.url')
    gitlab_project_name = serializers.ReadOnlyField(source='gitlab_project.name', allow_null=True)
    gitlab_project_path = serializers.ReadOnlyField(source='gitlab_project.path_with_namespace', allow_null=True)
    user_username = serializers.ReadOnlyField(source='user.username', allow_null=True)
    user_first_name = serializers.ReadOnlyField(source='user.first_name', allow_null=True)
    user_last_name = serializers.ReadOnlyField(source='user.last_name', allow_null=True)

    class Meta:
        model = GitLabAudit
        fields = [
            'id', 'gitlab_server', 'gitlab_server_name', 'gitlab_server_url',
            'gitlab_project', 'gitlab_project_name', 'gitlab_project_path',
            'user', 'user_username', 'user_first_name', 'user_last_name',
            'operation_type', 'target_name', 'description',
            'result', 'details', 'error_message', 'created_at'
        ]


class DevOpsTimelineSerializer(serializers.ModelSerializer):
    """DevOps操作时间线序列化器"""
    project_name = serializers.ReadOnlyField(source='project.name')
    environment_name = serializers.ReadOnlyField(source='environment.name')
    user_username = serializers.ReadOnlyField(source='user.username', allow_null=True)
    gitlab_project_name = serializers.ReadOnlyField(source='gitlab_project.name', allow_null=True)

    class Meta:
        model = DevOpsTimeline
        fields = [
            'id', 'project', 'project_name', 'environment', 'environment_name',
            'user', 'user_username', 'event_type', 'title', 'description',
            'gitlab_project', 'gitlab_project_name', 'status', 'details', 'created_at'
        ]
